package com.yz.yrpc.handler;

import com.yz.yrpc.util.ClassUtil;
import okhttp3.FormBody;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shanchong
 * @date 2019-07-09
 **/
public class DefaultParamHandler implements ParamHandler {

    @Override
    public Map<String,Object> handle(Object[] param) {
        final HashMap<String, Object> paramMap = new HashMap<>();
        for (int i = 0; i < param.length; i++) {
            final Object arg = param[i];
            final List<Method> methods = ClassUtil.getMethods(arg.getClass());
            for (Method m:methods){
                try {
                    if ("getClass".equals(m.getName())){
                        continue;
                    }
                    String name = m.getName().replace("get","");
                    paramMap.put(name.substring(0,1).toLowerCase()+ name.substring(1),
                            String.valueOf(m.invoke(arg)));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        return paramMap;
    }

}
